package com.yanqu.road.server.manager.orewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.UnionActivityRank;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.bussiness.rank.RankBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.logic.cross.CrossActivityUnionRankListModel;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossOreWarRankMgr extends TempMgr {

    private static Map<Integer, CrossActivityRankListModel> oreWarRankListModelMap;

    private static Map<Integer, CrossActivityUnionRankListModel> oreWarUnionRankListModelMap;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        for(CrossActivityRankListModel rankListModel : oreWarRankListModelMap.values()){
            if(rankListModel.isUpdate()){
                rankListModel.saveCrossOreWarRankList();
            }
        }
        for(CrossActivityUnionRankListModel rankListModel : oreWarUnionRankListModelMap.values()){
            if(rankListModel.isUpdate()){
                rankListModel.saveCrossOreWarUnionRankList();
            }
        }
        return true;
    }

    @Override
    public boolean reloadData() {
        oreWarRankListModelMap = new ConcurrentHashMap<>();
        initCrossRankListModelMap();
        oreWarUnionRankListModelMap = new ConcurrentHashMap<>();
        initCrossUnionRankListModelMap();
        return true;
    }

    private void initCrossRankListModelMap(){
        synchronized (oreWarRankListModelMap) {
            Map<Integer, ActivityInfo> activityInfoMap = CrossOreWarActivityMgr.getOpenActivityInfoMap();
            List<Integer> activityIdList = new ArrayList<>();
            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                activityIdList.add(activityInfo.getActivityId());
            }
            Map<Integer, LinkedList<CrossUserRank>> crossUserRankListMap = RankBussiness.getCrossOreWarRankListMap(activityIdList);
            for (Map.Entry<Integer, LinkedList<CrossUserRank>> dataEntry : crossUserRankListMap.entrySet()) {
                CrossActivityRankListModel rankListModel = new CrossActivityRankListModel(dataEntry.getKey(), true);
                ActivityInfo activityInfo = activityInfoMap.get(dataEntry.getKey());
                int conditionType = CrossOreWarActivityMgr.CONDITION_TYPE_USER;
                int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossOreWarActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                if (maxRank <= 0) {
                    maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                }
                rankListModel.setRankNum(maxRank);
                rankListModel.setRankList(dataEntry.getValue());
                oreWarRankListModelMap.put(dataEntry.getKey(), rankListModel);
            }
        }
    }

    private void initCrossUnionRankListModelMap(){
        synchronized (oreWarUnionRankListModelMap) {
            Map<Integer, ActivityInfo> activityInfoMap = CrossOreWarActivityMgr.getOpenActivityInfoMap();
            List<Integer> activityIdList = new ArrayList<>();
            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                activityIdList.add(activityInfo.getActivityId());
            }
            Map<Integer, LinkedList<UnionActivityRank>> crossUnionRankListMap = RankBussiness.getCrossOreWarUnionRankListMap(activityIdList);
            for (Map.Entry<Integer, LinkedList<UnionActivityRank>> dataEntry : crossUnionRankListMap.entrySet()) {
                CrossActivityUnionRankListModel rankListModel = new CrossActivityUnionRankListModel(dataEntry.getKey());
                ActivityInfo activityInfo = activityInfoMap.get(dataEntry.getKey());
                int conditionType = CrossOreWarActivityMgr.CONDITION_TYPE_UNION;
                int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossOreWarActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                if (maxRank <= 0) {
                    maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                }
                rankListModel.setRankNum(maxRank);
                rankListModel.setRankList(dataEntry.getValue());
                oreWarUnionRankListModelMap.put(dataEntry.getKey(), rankListModel);
            }
        }
    }

    public static CrossActivityRankListModel getCrossActivityRankListModel(int activityId){
        return oreWarRankListModelMap.get(activityId);
    }

    public static CrossActivityUnionRankListModel getCrossActivityUnionRankListModel(int activityId){
        return oreWarUnionRankListModelMap.get(activityId);
    }

    public static void changeOreWarRank(int activityId, CrossUserRank crossUserRank){
        CrossActivityRankListModel rankListModel = getCrossActivityRankListModel(activityId);
        if(null == rankListModel){
            synchronized (oreWarRankListModelMap){
                if(!oreWarRankListModelMap.containsKey(activityId)){
                    CrossOreWarActivityMgr.loadActivity();
                    ActivityInfo activityInfo = CrossOreWarActivityMgr.getOpenActivityInfo(activityId);
                    if(null != activityInfo){
                        CrossActivityRankListModel crossActivityRankListModel = new CrossActivityRankListModel(activityInfo.getActivityId(), true);
                        int conditionType = CrossOreWarActivityMgr.CONDITION_TYPE_USER;
                        int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossOreWarActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                        if(maxRank <= 0){
                            maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                        }
                        crossActivityRankListModel.setRankNum(maxRank);
                        oreWarRankListModelMap.put(activityId, crossActivityRankListModel);
                    }
                }
            }
            rankListModel = getCrossActivityRankListModel(activityId);
        }
        if(null != rankListModel){
            rankListModel.rankChange(crossUserRank);
        }
    }

    public static void changeOreWarUnionRank(int activityId, UnionActivityRank unionRank,boolean isUnionDel){
        CrossActivityUnionRankListModel rankListModel = getCrossActivityUnionRankListModel(activityId);
        if(null == rankListModel){
            synchronized (oreWarUnionRankListModelMap){
                if(!oreWarUnionRankListModelMap.containsKey(activityId)){
                    CrossOreWarActivityMgr.loadActivity();
                    ActivityInfo activityInfo = CrossOreWarActivityMgr.getOpenActivityInfo(activityId);
                    if(null != activityInfo){
                        CrossActivityUnionRankListModel crossActivityUnionRankListModel = new CrossActivityUnionRankListModel(activityInfo.getActivityId());
                        int conditionType = CrossOreWarActivityMgr.CONDITION_TYPE_UNION;
                        int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossOreWarActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                        if(maxRank <= 0){
                            maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                        }
                        crossActivityUnionRankListModel.setRankNum(maxRank);
                        oreWarUnionRankListModelMap.put(activityId, crossActivityUnionRankListModel);
                    }
                }
            }
            rankListModel = getCrossActivityUnionRankListModel(activityId);
        }
        if(null != rankListModel){
            if(isUnionDel){
                rankListModel.removeRank(unionRank.getUnionUid());
            }else {
                rankListModel.rankChange(unionRank);
            }
        }
    }

    public static int getCrossActivityRank(int activityId, long serverId, long userId) {
        int rank = -1;
        CrossActivityRankListModel rankListModel = getCrossActivityRankListModel(activityId);
        if(null != rankListModel){
            rank = rankListModel.getMyRank(userId, serverId);
        }
        int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
        ActivityInfo activityInfo = CrossOreWarActivityMgr.getOpenActivityInfo(activityId);
        if(null != activityInfo) {
            int conditionType = CrossOreWarActivityMgr.CONDITION_TYPE_USER;
            maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossOreWarActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
        }
        if(rank > maxRank){
            rank = -1;
        }
        return rank;
    }

    public static int getCrossActivityUnionRank(int activityId, long serverId, String unionUid) {
        int rank = -1;
        CrossActivityUnionRankListModel rankListModel = getCrossActivityUnionRankListModel(activityId);
        if(null != rankListModel){
            rank = rankListModel.getMyRank(serverId, unionUid);
        }
        int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
        ActivityInfo activityInfo = CrossOreWarActivityMgr.getOpenActivityInfo(activityId);
        if(null != activityInfo) {
            int conditionType = CrossOreWarActivityMgr.CONDITION_TYPE_UNION;
            maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossOreWarActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
        }
        if(rank > maxRank){
            rank = -1;
        }
        return rank;
    }
}